<?php
class SubscribeMemberAction extends UserAction{

	public function index(){
		
		$where['token'] = session('token');
		$where['issubscribe'] = 1;
		
		$qrtypeid = $this->_get('typeid');
		if($qrtypeid){
			$where['qrtype'] = $qrtypeid;
		}
		
		if (!mb_check_encoding($_GET['city'], 'utf-8')){
			$city = iconv('gbk', 'utf-8', $_GET['city']);
		}else{
			$city = $this->_get('city');
		}
		
		if($city){
			$where['city'] = $city;
		}
		
		$subscriber = M('subscriber');
		
		$count=$subscriber->where($where)->count();
		$page=new Page($count,10);
		$subscribers=$subscriber->where($where)->limit($page->firstRow.','.$page->listRows)->select();
		
		//$group = M('subscribe_group')->where($where)-> field('groupid, groupname')->select();
		/*
		foreach ($group as $key=>$val){
			$g[$val['groupid']] = $val['groupname'];
		}
		unset($group);
		$this->assign('g', $g);*/
		
		$citys = $subscriber->Distinct(true)->field('city')->select();
		$this->assign('citys',$citys);
		
		$qrtype = M('qrcode_type')->select();
		
		foreach ($qrtype as $key=>$val){
			$q[$val['id']] = $val['name'];
		}
		
		$this->assign('sqrtype',$qrtype);
		$this->assign('qtype',$q);
		$this->assign('subscribers', $subscribers);
		$this->assign('page',$page->show());
		
		C('TOKEN_ON',false);
		$this->display();
	}
	
	public function update(){
		$info = '更新完成，请稍候';
		$flag = true;
		$userlist = WechatAdvanceAPI::GetUserList();
		
		try {
			load('@.SubscribeHelper');
			if($userlist->total > 0){
				$openids = $userlist->data->openid;
				
				 foreach ($openids as $openId){
				 	//$flag = WechatAdvanceAPI::SaveUserinfo($openId);
				 	SaveUserinfo($openId);
				 }
			}else{
				$flag = false;
			}
		} catch (Exception $e) {
			Log::write('更新关注者错误：'.$e->getMessage(), Log::WARN);
			$flag = false;
		}
		
		if(!$flag){
			$info = '更新失败，你可以去微信后台查看具体信息';
		}
		
		$this->success($info, U(MODULE_NAME.'/index'));
	}
}

?>